//返回订单数据
const cartData = {
    aCartData: !!localStorage['cartData'] ? JSON.parse(localStorage['cartData']) : [],
    total: !!localStorage['total'] ? localStorage['total'] : 0,
    freight: !!localStorage['freight'] ? localStorage['freight'] : 0,
}
function cartReducer(state=cartData, action){
    switch(action.type){
        case 'addCart':
            addCart(state, action);
            return Object.assign({}, state, action.data);
        case 'deleteItem':
            deleteItem(state, action);
            return Object.assign({}, state, action.data);
        case 'check':
            checkItem(state, action);
            return Object.assign({}, state, action.data);
        case 'checkAll':
            checkAll(state, action);
            return Object.assign({}, state, action.data);
        case 'incAmount':
            incAmount(state, action);
            return Object.assign({}, state, action.data);
        case 'decAmount':
            decAmount(state, action);
            return Object.assign({}, state, action.data);
        case 'changeAmount':
            changeAmount(state, action);
            return Object.assign({}, state, action.data);       
        default:
            return state;
        
    }
}

//添加商品
function addCart(state, action){
    let bSameItem = false;
    const { aCartData } = state;
    if(aCartData.length > 0){
        for(let i = 0, length = aCartData.length; i < length; i++){
            if(aCartData[i].gid === action.data.gid && JSON.stringify(aCartData[i].attrs) === JSON.stringify(action.data.attrs)){
                aCartData[i].amount+=action.data.amount;
                bSameItem = true;
                break;
            }
        }
    }
    if(!bSameItem){
        aCartData.push(action.data);
    }
    setTotal(state);
    setFreight(state);
    localStorage['cartData'] = JSON.stringify(aCartData);
}

//删除商品
function deleteItem(state, action){
    const { index } = action.data;
    state.aCartData.splice(index, 1);
    setTotal(state);
    setFreight(state);
    localStorage['cartData'] = JSON.stringify(state.aCartData);
}

//选择商品
function checkItem(state, action){
    const { index, checked } = action.data;
    state.aCartData[index].checked = !checked;
    setTotal(state);
    setFreight(state);
    localStorage['cartData'] = JSON.stringify(state.aCartData);
}

//设置全选
function checkAll(state, action){
    const { checked } = action.data;
    state.aCartData.map(item => item.checked = checked);
    setTotal(state);
    setFreight(state);
    localStorage['cartData'] = JSON.stringify(state.aCartData);
}

//增加数量
function incAmount(state, action){
    const { index } = action.data;
    state.aCartData[index].amount += 1;
    setTotal(state);
    localStorage['cartData'] = JSON.stringify(state.aCartData);
}

//减少数量
function decAmount(state, action){
    const { index } = action.data;
    if(state.aCartData[index].amount > 1){
        state.aCartData[index].amount -= 1;
        setTotal(state);
        localStorage['cartData'] = JSON.stringify(state.aCartData);
    }
}

//数量改变
function changeAmount(state, action){
    const { index, value } = action.data;
    state.aCartData[index].amount = Number(value);
    setTotal(state);
    localStorage['cartData'] = JSON.stringify(state.aCartData);
}

//计算运费
function setFreight(state){
    const aFreight = [];
    state.aCartData.map(item => {
        item.checked && aFreight.push(item.freight);
    });
    state.freight = aFreight.length > 0 ? Math.max.apply(null, aFreight) : 0;
    localStorage['freight'] = state.freight;
}

//重置商品合计价格
function setTotal(state){
    let total = 0;
    state.aCartData.map(item => {
        if(item.checked){ total += (item.price*item.amount); }
    });
    total = parseFloat(Math.round(total).toFixed(2));
    state.total = total;
    localStorage['total'] = JSON.stringify(total);
}

export default cartReducer;